package controller

import (
	"gin/common"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func SentenceSelect(ctx *gin.Context) {
	var selectDictQuery validate.SelectSentenceQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	total, resData, queryErr := service.SelectSentence(&selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 查询单条词库
func SentenceFind(ctx *gin.Context) {
	var selectDictQuery validate.SelectSentenceQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	resData, queryErr := service.FindSentence(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 插入日志
func SentenceCreate(ctx *gin.Context) {
	var addSentence validate.AddSentenceWrapper

	err := ctx.ShouldBindJSON(&addSentence)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.CreateSentence(addSentence.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func SentenceUpdate(ctx *gin.Context) {
	var updateSentence validate.UpdateSentenceWrapper

	err := ctx.ShouldBindJSON(&updateSentence)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.UpdateSentence(updateSentence.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func SentenceDelete(ctx *gin.Context) {
	var deleteSentenceParam validate.DeleteSentenceParam

	err := ctx.ShouldBindJSON(&deleteSentenceParam)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateFilterParam(&deleteSentenceParam.Param, "sentence")

	service.DeleteSentence(&selectQueryParam)

	// 查询成功
	response.Success(ctx, nil, "success")

}

// 分析段落并保存结果
func SentenceLexicalAnalysis(ctx *gin.Context) {
	var selectDictQuery validate.LexicalAnalysisSentence

	jsonErr := ctx.ShouldBindJSON(&selectDictQuery)

	if jsonErr != nil {
		response.Fail(ctx, nil, jsonErr.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	data, err := service.LexicalAnalysisSentence(&selectQueryParam, selectDictQuery.DictId, selectDictQuery.ReplaceOldResult, selectDictQuery.IsAsync == "yes", selectDictQuery.IsSentimentAnalysis == "yes", true)

	if err != nil {
		// 操作失败
		response.Fail(ctx, nil, err.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": data}, "success")
	}
}

// 句子情感分析
func SentenceSentimentAnalysis(ctx *gin.Context) {
	var selectDictQuery validate.SentimentAnalysisSentence

	jsonErr := ctx.ShouldBindJSON(&selectDictQuery)

	if jsonErr != nil {
		response.Fail(ctx, nil, jsonErr.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	data, err := service.SentimentAnalysisSentence(&selectQueryParam, selectDictQuery.IsAsync == "yes")

	if err != nil {
		// 操作失败
		response.Fail(ctx, nil, err.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": data}, "success")
	}
}

// 新增段落并保存结果
func SentenceAddAndLexicalAnalysis(ctx *gin.Context) {
	var selectDictQuery validate.SentenceAddAndLexicalAnalysis

	jsonErr := ctx.ShouldBindJSON(&selectDictQuery)

	if jsonErr != nil {
		response.Fail(ctx, nil, jsonErr.Error())
		return
	}

	data, matchEntity, err := service.AddAndLexicalAnalysisSentence(&selectDictQuery)

	if err != nil {
		// 操作失败
		response.Fail(ctx, nil, err.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"lexical_analysis": data, "match": matchEntity}, "success")
	}
}
